//VPA:18 Creation IHM Gerer parcours

package vue.edition;

import javax.swing.JPanel;
import javax.swing.BoxLayout;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JLabel;

import java.awt.FlowLayout;

import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Font;

import javax.swing.border.EmptyBorder;
import javax.swing.JList;
import javax.swing.border.LineBorder;

import java.awt.SystemColor;
import java.awt.Component;

import javax.swing.SwingConstants;

import java.awt.CardLayout;
import java.awt.Dimension;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextArea;

import controler.consultation.ConsultLieu;
import controler.edition.EditLieu;
import controler.edition.EditParcours;

import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JComboBox;
import javax.swing.UIManager;

public class GererParcours extends JPanel {
	
	private JPanel contentPane;
	private JPanel panel;
	private JTextField textField;
	private JPanel panelListPoi;
	private JPanel panel_7;
	private JPanel panel_9;
	private JPanel panelListParcours;
	private JPanel panel_8;
	private JPanel panel_1;
	private JPanel panel_2;
	private JButton btnAjouter;
	private JPanel panel_3;
	private JPanel panel_4;
	private JLabel lblLibell;
	private JPanel panel_5;
	private JLabel lblDescription;
	private JTextArea textArea;
	private JLabel lblLieu;
	private JComboBox<String> comboBox;
	private JButton btnActualiser;
	private JButton btnModifier;
	private JButton btnSupprimer;
	private JList<String> listParcours;
	private JList<String> listPoi;
	private JPanel panel_6;
	private JPanel panel_10;
	private JPanel panel_11;
	private JLabel lblRechercher;
	private JTextField textFieldSearch;
	private JButton btnOk;
	private JRadioButton rdbtnParMotCle;
	private JRadioButton rdbtnParNomExact;
	private ButtonGroup grouprdbtn;

	public GererParcours(JPanel contentPane) {
		this.contentPane = contentPane;
	}
	
	public void initComponents() {
		panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "G\u00E9rer les Parcours", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		panel_7 = new JPanel();
		panel.add(panel_7, BorderLayout.NORTH);
		panel_7.setLayout(new BoxLayout(panel_7, BoxLayout.Y_AXIS));
		
		panel_10 = new JPanel();
		panel_7.add(panel_10);
		
		lblRechercher = new JLabel("Rechercher :");
		panel_10.add(lblRechercher);
		
		textFieldSearch = new JTextField();
		panel_10.add(textFieldSearch);
		textFieldSearch.setColumns(10);
		
		btnOk = new JButton("OK");
		panel_10.add(btnOk);
		
		panel_11 = new JPanel();
		panel_7.add(panel_11);
		
		rdbtnParMotCle = new JRadioButton("Par Mots Cl\u00E9s");
		rdbtnParMotCle.setSelected(true);
		panel_11.add(rdbtnParMotCle);
		
		rdbtnParNomExact = new JRadioButton("Par Nom Exact");
		panel_11.add(rdbtnParNomExact);
		
		grouprdbtn = new ButtonGroup();
		grouprdbtn.add(rdbtnParMotCle);
		grouprdbtn.add(rdbtnParNomExact);
		
		panel_9 = new JPanel();
		panel.add(panel_9, BorderLayout.CENTER);
		panel_9.setLayout(new BorderLayout(0, 0));
		
		panelListParcours = new JPanel();
		panel_9.add(panelListParcours, BorderLayout.CENTER);
		panelListParcours.setLayout(new BorderLayout(0, 0));
		
		listParcours = new JList<>(new EditParcours().listerParcours());
		panelListParcours.add(listParcours);
		
		panelListPoi = new JPanel();
		panel_9.add(panelListPoi, BorderLayout.SOUTH);
		panelListPoi.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Point d'Interets :", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelListPoi.setLayout(new BorderLayout(0, 0));
		panelListPoi.setVisible(false);
		
		listPoi = new JList<>();
		listPoi.setEnabled(false);
		panelListPoi.add(listPoi);
		
		panel_8 = new JPanel();
		panel.add(panel_8, BorderLayout.SOUTH);
		
		btnActualiser = new JButton("Actualiser");
		panel_8.add(btnActualiser);
		
		btnModifier = new JButton("Modifier");
		panel_8.add(btnModifier);
		
		btnSupprimer = new JButton("Supprimer");
		panel_8.add(btnSupprimer);
		
		panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Ajouter un Parcours", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		panel_2 = new JPanel();
		panel_1.add(panel_2, BorderLayout.SOUTH);
		
		btnAjouter = new JButton("Ajouter");
		panel_2.add(btnAjouter);
		
		panel_3 = new JPanel();
		panel_1.add(panel_3, BorderLayout.CENTER);
		panel_3.setLayout(new BoxLayout(panel_3, BoxLayout.Y_AXIS));
		
		panel_4 = new JPanel();
		FlowLayout fl_panel_4 = (FlowLayout) panel_4.getLayout();
		fl_panel_4.setAlignment(FlowLayout.LEFT);
		panel_3.add(panel_4);
		
		lblLibell = new JLabel("Libell\u00E9 :");
		panel_4.add(lblLibell);
		
		textField = new JTextField();
		panel_4.add(textField);
		textField.setColumns(10);
		
		panel_5 = new JPanel();
		panel_3.add(panel_5);
		panel_5.setLayout(new BorderLayout(0, 0));
		
		lblDescription = new JLabel("Description :");
		panel_5.add(lblDescription, BorderLayout.NORTH);
		
		textArea = new JTextArea();
		panel_5.add(textArea, BorderLayout.CENTER);
		
		panel_6 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_6.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		panel_3.add(panel_6);
		
		lblLieu = new JLabel("Lieu :");
		panel_6.add(lblLieu);
		
		comboBox = new JComboBox<>();
		new ConsultLieu().listerLieu(comboBox);
		panel_6.add(comboBox);
		
		listeners();
	}
	
	public void refreshList() {
		contentPane.setVisible(false);
		contentPane.removeAll();
		initComponents();
		contentPane.setVisible(true);
	}
	
	public void listeners() {
		
		btnOk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(rdbtnParMotCle.isSelected()) {
					listParcours.setModel(new EditParcours().searchKeyWord(textFieldSearch.getText()));
				} else if(rdbtnParNomExact.isSelected()) {
					listParcours.setModel(new EditParcours().searchExactWord(textFieldSearch.getText()));
				}
			}
		});
		
		listParcours.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				contentPane.setVisible(false);
				String[] split = listParcours.getSelectedValue().split(" | ");
				int id = Integer.parseInt(split[0]);
				
				listPoi.setModel(new EditParcours().listerPoi(id));
				panelListPoi.setVisible(true);
				contentPane.setVisible(true);
			}
		});
	
		btnAjouter.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String[] split = comboBox.getSelectedItem().toString().split(" - ");
				int id = Integer.parseInt(split[0]);
				
				new EditParcours().ajouterParcours(textField.getText(), textArea.getText(), id);
				textField.setText(null);
				textArea.setText(null);
				refreshList();
			}
		});
		
		btnActualiser.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		btnModifier.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		btnSupprimer.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (listParcours.getSelectedValue() != null) {
					String[] split = listParcours.getSelectedValue().split(" | ");
					int id = Integer.parseInt(split[0]);
					
					new EditParcours().delete(id);
					refreshList();
				}
			}
		});
	}
	
}
